import pygame
import datamap

def blend_color(image, color):
    sf = pygame.Surface(image.get_rect().size)
    sf.fill(color)
    image.blit(sf, (0, 0), sf.get_rect(), pygame.BLEND_MULT)

def load_image(file, colorkey = None): # file exist
    image = pygame.image.load(file).convert()
    if colorkey: image.set_colorkey(colorkey, pygame.RLEACCEL)
    return image

def load_frame(file, (sx, sy), colorkey = None):
    image = pygame.image.load(file).convert()
    if colorkey is not None: image.set_colorkey(colorkey, pygame.RLEACCEL)
    allrect = image.get_rect()
    width = (allrect.width) // sx
    height = (allrect.height) // sy
    frame = []
    for y in range(0, allrect.height, height):
        for x in range(0, allrect.width, width):
            sub = pygame.Surface((width, height))
            sub.blit(image, (0, 0), (x, y, width, height))
            if colorkey: sub.set_colorkey(colorkey, pygame.RLEACCEL)
            frame.append(sub)
    return tuple(frame)

def node2pixel(node):
    return (node[0] * datamap.DataMap.GRID, node[1] * datamap.DataMap.GRID)
    
def pixel2node(pixel):
    return (int(pixel[0] // datamap.DataMap.GRID), int(pixel[1] // datamap.DataMap.GRID))